﻿using System;
using System.Collections.Generic;
using System.Linq;
using Biz.EF.Platform.Kq;
using Biz.Properties;

namespace Biz.Buffer
{
    public static class CommonInformation
    {
        private static DateTime _lastLoadTime = DateTime.Now;
        private static List<Envi_PS_Waste_Water_Let_Port> _letports;
        /// <summary>
        /// 根据浙江省在线平台在线设备号查询排口信息
        /// </summary>
        /// <param name="onlineNumber">浙江省在线平台在线设备号</param>
        /// <returns>排口信息</returns>
        public static Envi_PS_Waste_Water_Let_Port GetLetportByOnlineNum(string onlineNumber)
        {
            if (_letports == null || (DateTime.Now - _lastLoadTime).TotalMinutes > 10)
            {
                using (var pCtx = new PlatformYgContext())
                {
                    _letports = pCtx.Envi_PS_Waste_Water_Let_Port.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            return _letports?.FirstOrDefault(t => t.online_inspect_Number.Equals(onlineNumber));
        }
        public static List<Envi_PS_Waste_Water_Let_Port> GetAllLetports()
        {
            if (_letports == null || (DateTime.Now - _lastLoadTime).TotalMinutes > 10)
            {
                using (var pCtx = new PlatformYgContext())
                {
                    _letports = pCtx.Envi_PS_Waste_Water_Let_Port.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            return _letports;
        }
        /// <summary>
        /// 根据MN号查询排口信息
        /// </summary>
        /// <param name="mn">mn</param>
        /// <returns>排口信息</returns>
        public static Envi_PS_Waste_Water_Let_Port GetLetportByMn(string mn)
        {
            if (_letports == null || (DateTime.Now - _lastLoadTime).TotalMinutes > 10)
            {
                using (var pCtx = new PlatformYgContext())
                {
                    _letports = pCtx.Envi_PS_Waste_Water_Let_Port.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            return _letports?.FirstOrDefault(t => t.Data_Collection_Instrument_SN.Equals(mn));
        }
        public static Envi_PS_Waste_Water_Let_Port GetLetportById(int? id)
        {
            if (_letports == null || (DateTime.Now - _lastLoadTime).TotalMinutes > 10)
            {
                using (var pCtx = new PlatformYgContext())
                {
                    _letports = pCtx.Envi_PS_Waste_Water_Let_Port.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            return _letports?.FirstOrDefault(t => t.ID.Equals(id));
        }
        private static List<Envi_PS_Basic_Info> _pollutionSources;
        /// <summary>
        /// 根据污染源ID获取污染源信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>污染源信息</returns>
        public static Envi_PS_Basic_Info GetPollutionSourceById(int id)
        {
            if (_pollutionSources == null || (DateTime.Now - _lastLoadTime).TotalMinutes > 10)
            {
                using (var pCtx = new PlatformYgContext())
                {
                    _pollutionSources = pCtx.Envi_PS_Basic_Info.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            return _pollutionSources?.FirstOrDefault(t => t.ID == id);
        }
        private static List<Envi_PS_Water_Contam_Let_Licence> _licences;
        public static List<Envi_PS_Water_Contam_Let_Licence> GetAllLicencess()
        {
            if (_licences == null || (DateTime.Now - _lastLoadTime).TotalMinutes > 10)
            {
                using (var pCtx = new PlatformYgContext())
                {
                    _licences = pCtx.Envi_PS_Water_Contam_Let_Licence.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            return _licences;
        }
        private static Dictionary<string, string> _receiverInfoes;
        public static Dictionary<string, string> GetReceiverInfoes()
        {
            if (_receiverInfoes == null)
            {
                try
                {
                    _receiverInfoes = new Dictionary<string, string>();
                    var msg = Settings.Default.ReceiverInfoes.Replace('；', ';');
                    var persons = msg.Split(';');
                    foreach (var person in persons)
                    {
                        var nameMobile = person.Split('=');
                        if (nameMobile.Length == 2)
                        {
                            _receiverInfoes.Add(nameMobile[0], nameMobile[1]);
                        }

                    }
                }
                catch (Exception ex)
                {
                    //todo:
                }

            }
            return _receiverInfoes;
        }

        private static List<Envi_PS_SCommunication> _communications;
        public static List<Envi_PS_SCommunication> GetAllCommunications()
        {
            if (_communications == null || (DateTime.Now - _lastLoadTime).TotalMinutes > 10)
            {
                using (var pCtx = new PlatformYgContext())
                {
                    _communications = pCtx.Envi_PS_SCommunication.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            return _communications;
        }

    }
}
